import {
	createStore
} from 'vuex';

import Util from '../common/util.js'
import Cache from '../common/cache.js'

// modules
import UserModule from './module/user.js'
import GiftModule from './module/gift.js'
import CardModule from './module/card.js'

const store = createStore({
	state: {
		lockData: {},
		scenario: null,
		accessToken: null,
		activityId: null,
		cardId: null,
		cardKey: null,
		authKey: null,
		globalConfig: null,
		cardConfig: null

	},
	modules: {
		user: UserModule,
		gift: GiftModule,
		card: CardModule,
	},
	mutations: {
		lock(state, key) {
			state.lockData[key] = Util.getTimestamp(null) + 5
		},
		unlock(state, key) {
			state.lockData[key] = Util.getTimestamp(null) + 2
		},
		setScenario(state, scenario) {
			state.scenario = scenario
			Cache.put('scenario', scenario)
		},
		delScenario(state) {
			state.scenario = null
			Cache.remove('scenario')
		},
		setAccessToken(state, accessToken) {
			state.accessToken = accessToken
			Cache.put('accessToken', accessToken)
		},
		delAccessToken(state) {
			state.accessToken = null
			Cache.remove('accessToken')
		},
		setActivityId(state, activityId) {
			state.activityId = activityId
			Cache.put('activityId', activityId)
		},
		delActivityId(state) {
			state.activityId = null
			Cache.remove('activityId')
		},
		setGlobalConfig(state, config) {
			state.globalConfig = config
			Cache.put('globalConfig', config) // 缓存5分钟
		},
		setCardConfig(state, config) {
			state.cardConfig = config
			Cache.put('cardConfig', config) // 缓存5分钟
		},
		setCardId(state, cardId) {
			state.cardId = cardId
			Cache.put('cardId', cardId)
		},
		delCardId(state) {
			state.cardId = null
			Cache.remove('cardId')
		},
		setCardKey(state, cardKey) {
			state.cardKey = cardKey
			Cache.put('cardKey', cardKey)
		},
		delCardKey(state) {
			state.cardKey = null
			Cache.remove('cardKey')
		},
		setAuthKey(state, authKey) {
			state.authKey = authKey
			Cache.put('authKey', authKey)
		},
		delAuthKey(state) {
			state.authKey = null
			Cache.remove('authKey')
		},
	},
	getters: {
		isLock(state, key) {
			return key => {
				const lockTime = state.lockData[key]
				const timestamp = Util.getTimestamp(null)
				console.log('isLock', key, lockTime, timestamp)
				if (lockTime && lockTime > timestamp) {
					return true
				}
				return false
			}
		},
		scenario(state) {
			if (state.scenario === null) {
				state.scenario = Cache.get('scenario') || ''
			}
			return state.scenario
		},
		accessToken(state) {
			if (state.accessToken === null) {
				state.accessToken = Cache.get('accessToken') || ''
			}
			return state.accessToken
		},
		activityId(state) {
			if (state.activityId === null) {
				state.activityId = Cache.get('activityId') || 0
			}
			return state.activityId
		},
		isLogin(state) {
			if (state.accessToken === null) {
				state.accessToken = Cache.get('accessToken')
			}
			return !!state.accessToken
		},
		globalConfig(state) {
			if (state.globalConfig === null) {
				const globalConfig = Cache.get('globalConfig')
				state.globalConfig = globalConfig ? globalConfig : {}
			}
			return state.globalConfig
		},
		cardConfig(state) {
			if (state.cardConfig === null) {
				const cardConfig = Cache.get('cardConfig')
				state.cardConfig = cardConfig ? cardConfig : {}
			}
			return state.cardConfig
		},
		cardId(state) {
			if (state.cardId === null) {
				state.cardId = Cache.get('cardId') || 0
			}
			return state.cardId
		},
		cardKey(state) {
			if (state.cardKey === null) {
				state.cardKey = Cache.get('cardKey') || ''
			}
			return state.cardKey
		},
		authKey(state) {
			if (state.authKey === null) {
				state.authKey = Cache.get('authKey') || ''
			}
			return state.authKey
		},
	}
})
export default store
